package com.espirit.smc.client;

import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.espirit.smc.ServerMangerProperties;

public class ClientProperties extends ServerMangerProperties {

	private String scanPaths;

	private String temp;

	private Set<Path> clientPaths;
	
	private String serverURL;
	
	private String serverRoot;
	
	private String watchPaths;

	private Set<Path> serverPaths;
	
	private String serverIgnoreSubDirPaths;
	
	private  Set<Path> serverIgnoreDirPaths;

	public Set<Path> getScanPaths() {
		if (this.clientPaths == null && StringUtils.isNotBlank(getRoot()) && StringUtils.isNotBlank(scanPaths)) {
			this.clientPaths = new LinkedHashSet<Path>();
			String[] ps = scanPaths.split(";");
			for (String p : ps) {
				if (p.startsWith(getRoot())) {
					this.clientPaths.add(fs.getPath(p));
				} else {
					Path fullPath = fs.getPath(getRoot(), p);
					this.clientPaths.add(fullPath);
				}
			}
		}
		return clientPaths;
	}

	public void setScanPaths(String scanPaths) {
		this.scanPaths = scanPaths;
		this.clientPaths = null;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}

	public String getServerURL() {
		return serverURL;
	}

	public void setServerURL(String serverURL) {
		this.serverURL = serverURL;
	}

	public Path getTempPath() {
		return StringUtils.isBlank(temp) ? null : fs.getPath(temp);
	}

	public String getServerRoot() {
		return serverRoot;
	}

	public void setServerRoot(String serverRoot) {
		this.serverRoot = serverRoot;
		this.watchPaths = null;
	}
	
	public Path getServerRootPath(){
		return StringUtils.isBlank(serverRoot) ? null : fs.getPath(serverRoot);
	}

	public Set<Path> getWatchPaths() {
		if (this.serverPaths == null && StringUtils.isNotBlank(serverRoot) && StringUtils.isNotBlank(watchPaths)) {
			this.serverPaths = new LinkedHashSet<Path>();
			String[] ps = watchPaths.split(";");
			for (String p : ps) {
				if (p.startsWith(serverRoot)) {
					this.serverPaths.add(fs.getPath(p));
				} else {
					Path fullPath = fs.getPath(serverRoot, p);
					this.serverPaths.add(fullPath);
				}
			}
		}
		return serverPaths;
	}

	public void setWatchPaths(String watchPaths) {
		this.watchPaths = watchPaths;
	}

	public Set<Path> getServerIgnoreSubDirPaths() {
		if (this.serverIgnoreSubDirPaths == null && StringUtils.isNotBlank(serverRoot) && StringUtils.isNotBlank(serverIgnoreSubDirPaths)) {
			this.serverIgnoreDirPaths = new LinkedHashSet<Path>();
			String[] ps = serverIgnoreSubDirPaths.split(";");
			for (String p : ps) {
				if (p.startsWith(serverRoot)) {
					this.serverIgnoreDirPaths.add(fs.getPath(p));
				} else {
					Path fullPath = fs.getPath(serverRoot, p);
					this.serverIgnoreDirPaths.add(fullPath);
				}
			}
		}
		return serverIgnoreDirPaths;
	}

	public void setServerIgnoreSubDirPaths(String serverIgnoreSubDirPaths) {
		this.serverIgnoreSubDirPaths = serverIgnoreSubDirPaths;
	}
	
	
	
	
}
